/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.stat;

import java.util.Vector;
import jpcap.packet.Packet;
import tnviet.proj.jna.JNPacketAnalyzerLoader;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.analyzer.JNPacketAnalyzer;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class ApplicationProtocolStat extends JNStatisticsController{

    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);
    JNPacketAnalyzer[] analyzers;
    long[] numberOfPackets;
    long totalPackets;
    long[] sizeOfPackets;
    long totalSize;
    String[] labels;

    final String[] types = {
        resourceManager.getString("packetsNumber"),
        resourceManager.getString("packetsPercent"),
        resourceManager.getString("totalPacketsSize"),
        resourceManager.getString("packetsSizePercent")
    };


    public ApplicationProtocolStat() {
        analyzers = JNPacketAnalyzerLoader.getAnalyzersOf(JNPacketAnalyzer.APPLICATION_LAYER);
        numberOfPackets = new long[analyzers.length + 1];
        sizeOfPackets = new long[analyzers.length + 1];
        labels = new String[analyzers.length + 1];
        for(int i  = 0; i< analyzers.length; i++){
            labels[i]  = analyzers[i].getProtocolName();
        }
        labels[analyzers.length] = resourceManager.getString("other");
    }

    @Override
    public String getName() {
        return resourceManager.getString("applicationRatio");
    }

    @Override
    public void analyze(Vector packets) {
        for(int i=0;i<packets.size();i++){
            Packet p = (Packet)packets.elementAt(i);
            totalPackets++;
            boolean flag = false;
            for(int j=0; j<analyzers.length;j++){
                if(analyzers[j].isAnalyzable(p)){
                    numberOfPackets[j]++;
                    sizeOfPackets[j]+=((Packet)p).len;
                    totalSize += ((Packet)p).len;
                    flag = true;
                    break;
                }
            }
            if(!flag){
                numberOfPackets[numberOfPackets.length-1]++;
                sizeOfPackets[numberOfPackets.length-1]+=p.len;
                totalSize+=p.len;
            }
        }
    }

    @Override
    public void addPacket(Packet p) {
        boolean flag = false;
        totalPackets++;
        for(int j=0; j<analyzers.length;j++){
            if(analyzers[j].isAnalyzable(p)){
                numberOfPackets[j]++;
                sizeOfPackets[j]+=((Packet)p).len;
                totalSize += ((Packet)p).len;
                flag = true;
                break;
            }
        }
        if(!flag){
            numberOfPackets[numberOfPackets.length-1]++;
            sizeOfPackets[numberOfPackets.length-1]+=p.len;
            totalSize+=p.len;
        }
    }

    @Override
    public String[] getLabels() {
        return labels;
    }

    @Override
    public String[] getStatTypes() {
        return types;
    }

    @Override
    public long[] getValues(int index) {
        switch(index){
            case 0:// number of packets
                if(numberOfPackets == null){
                    return new long[0];
                }
                return numberOfPackets;
            case 1:// pecentage of packets
                long[] percents = new long[numberOfPackets.length];
                if(totalPackets == 0) return percents;
                for(int i = 0; i< numberOfPackets.length; i++){
                    percents[i] = numberOfPackets[i]*100/totalPackets;
                }
                return percents;
            case 2: // total packetsize
                if(sizeOfPackets == null) return new long[0];
                return sizeOfPackets;
            case 3: // percentage of size
                long[] percents2 = new long[sizeOfPackets.length];
                if(totalSize == 0) return percents2;
                for(int i  = 0; i < sizeOfPackets.length; i++){
                    percents2[i] = sizeOfPackets[i]*100/totalSize;
                }
                return percents2;
            default:
                return null;
        }
    }

    @Override
    public void clear() {
        numberOfPackets = new long[analyzers.length + 1];
        sizeOfPackets = new long[analyzers.length + 1];
        totalPackets = 0;
        totalSize = 0;
    }

}
